package com.example.housekeeping.controller;

import com.example.housekeeping.pojo.dto.UserAddNewDTO;
import com.example.housekeeping.pojo.dto.UserUpdateDTO;
import com.example.housekeeping.pojo.entity.User;
import com.example.housekeeping.pojo.vo.UserListItemVO;
import com.example.housekeeping.pojo.vo.UserLoginVO;
import com.example.housekeeping.pojo.vo.UserStandardVO;
import com.example.housekeeping.service.UserService;
import com.example.housekeeping.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.util.List;

/**
 * 用户表(User)表控制层
 *
 * @author bob
 * @since 2023-03-30 14:26:07
 */
@Slf4j
@RestController
@RequestMapping("/user")
@Api(tags = "用户信息模块")
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("/add-new")
    @ApiOperation("添加用户信息")
    @ApiOperationSupport(order = 100)
    public JsonResult<Void> addNew(UserAddNewDTO userAddNewDTO) {
        log.debug("开始处理【新增用户信息】的请求，参数：{}", userAddNewDTO);
        userService.addNew(userAddNewDTO);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/delete")
    @ApiOperation("根据ID删除用户信息")
    @ApiOperationSupport(order = 200)
    public JsonResult<Void> deleteById(@PathVariable Long id) {
        log.debug("开始处理【删除用户信息】的请求，参数：{}", id);
        userService.deleteById(id);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/update")
    @ApiOperation("修改用户信息")
    @ApiOperationSupport(order = 300)
    public JsonResult<Void> updateById(@PathVariable Long id, UserUpdateDTO userUpdateDTO) {
        log.debug("开始处理【修改用户信息】的请求，参数：{}", id, userUpdateDTO);
        userService.updateById(id, userUpdateDTO);
        return JsonResult.ok();
    }

    @GetMapping("/{id:[0-9]+}")
    @ApiOperation("根据ID查询用户信息")
    @ApiOperationSupport(order = 400)
    public JsonResult<UserStandardVO> getStandardById(@PathVariable Long id) {
        log.debug("开始处理【根据ID查询用户详情】的请求，参数：{}", id);
        UserStandardVO queryResult = userService.getStandardById(id);
        return JsonResult.ok(queryResult);
    }

    //    @PostMapping("")
//    @ApiOperation("查询用户信息")
//    @ApiOperationSupport(order = 500)
//    public JsonResult<List<UserListItemVO>> list() {
//        log.debug("开始处理【查询用户信息】的请求，无参数");
//        List<UserListItemVO> list = userService.list();
//        return JsonResult.ok(list);
//    }
    @PostMapping("/login")
    @ApiOperation("登录")
    @ApiOperationSupport(order = 600)
    public JsonResult<UserLoginVO> login(UserAddNewDTO userAddNewDTO, HttpSession httpSession) {
        log.debug("进入登录");
        UserLoginVO login = userService.login(userAddNewDTO, httpSession);
        return JsonResult.ok(login);
    }

    @PostMapping("/register")
    @ApiOperation("注册")
    @ApiOperationSupport(order = 601)
    public JsonResult<Void> register(UserAddNewDTO userAddNewDTO) {
        log.debug("进入注册");
        userService.register(userAddNewDTO);
        return JsonResult.ok();
    }

    @RequestMapping("/currentUser")
    @ApiOperation("查看用户保存的数据")
    @ApiOperationSupport(order = 602)
    public JsonResult<UserLoginVO> currentUser(HttpSession session) {
        UserLoginVO user = (UserLoginVO) session.getAttribute("user");
        return JsonResult.ok(user);
    }

    @PostMapping("/logout")
    @ApiOperation("退出")
    @ApiOperationSupport(order = 603)
    public JsonResult<Void> logout(HttpSession session) {
     session.removeAttribute("user");
        return JsonResult.ok();
    }
}

